// 此文件将主表数据与申请表数据进行对比
import store from '@/store'
import { baseInfo, dateInfo, organInfo, investInfo, rateInfo, fundFileInfo, feeRateContrast } from './contrastData'
/*
property   对比属性
propertyChineName  文字标题
stepObjectName  步骤
*/
export function publicCompare(property, propertyChineName, stepObjectName) { // 对比对象属性
	// 组件初始化的时候禁止调用
	if (!store.state.fundStep.pucliktTotalData.productFundApply && !store.state.fundStep.pucliktTotalData.productFundInfo) {
		return
	}
	if (!isData(1, stepObjectName) && !isData(2, stepObjectName)) { // 当申请表和主表都没有数据
		return false
	}
	const productFundApply = store.state.fundStep.pucliktTotalData.productFundApply
	const productFundInfo = store.state.fundStep.pucliktTotalData.productFundInfo
	const applyData = productFundApply && productFundApply[stepObjectName] ? productFundApply[stepObjectName] : {}
	const infoData = productFundInfo && productFundInfo[stepObjectName] ? productFundInfo[stepObjectName] : {}
	const info = infoData[property] ? infoData[property] : ''
	const Apply = applyData[property] ? applyData[property] : ''
	if (info !== Apply) { // 当申请表与主表的属性值不相同时
		if (!store.state.fundStep.pulicAttribute.includes(propertyChineName)) {
			store.state.fundStep.pulicAttribute.push(propertyChineName)
			store.state.fundStep.pulicCheckNum[stepObjectName].num += 1
		}
		return true
	}
}

// 判断主表和申请表数据是否为空
/*
param
type 1   主表
type 2  申请表
stepPertyName   步骤对象字段名称
*/
function isData(type, stepPertyName) {
	if (type === 1) {
		if (store.state.fundStep.pucliktTotalData.productFundInfo) {
			return !!store.state.fundStep.pucliktTotalData.productFundInfo[stepPertyName]
		} else {
			return false
		}
	} else if (type === 2) {
		if (store.state.fundStep.pucliktTotalData.productFundApply) {
			return !!store.state.fundStep.pucliktTotalData.productFundApply[stepPertyName]
		} else {
			return false
		}
	}
}

// 对比下属基金数组又没有变化
export function publiArraycCompare() {
	// 组件初始化的时候禁止调用
	if (!store.state.fundStep.pucliktTotalData.productFundApply && !store.state.fundStep.pucliktTotalData.productFundInfo) {
		return false
	}
	const productFundApply = store.state.fundStep.pucliktTotalData.productFundApply
	const productFundInfo = store.state.fundStep.pucliktTotalData.productFundInfo
	const applyBaseInfo = productFundApply && productFundApply.baseInfo ? productFundApply.baseInfo : {}
	const infoBaseInfo = productFundInfo && productFundInfo.baseInfo ? productFundInfo.baseInfo : {}
	const applyBranchShareList = applyBaseInfo.branchShareList && Array.isArray(applyBaseInfo.branchShareList) ? applyBaseInfo.branchShareList : []
	const infoBranchShareList = infoBaseInfo.branchShareList && Array.isArray(infoBaseInfo.branchShareList) ? infoBaseInfo.branchShareList : []
	if (!applyBranchShareList.length && !infoBranchShareList.length) { // 如主表和申请表都没有下属基金，直接返回
		return false
	} else if (applyBranchShareList.length !== infoBranchShareList.length) { // 如数组长度不一样，直接返回
		if (!store.state.fundStep.pulicAttribute.includes('下属基金')) {
			store.state.fundStep.pulicAttribute.push('下属基金')
			store.state.fundStep.pulicCheckNum.baseInfo.num += 1
		}
		return true
	} else if (applyBranchShareList.length === infoBranchShareList.length) {
		for (let index = 0; index < applyBranchShareList.length; index++) {
			const keys = Object.keys(applyBranchShareList[index])
			let flag = false
			keys.forEach(item => {
				const apply = applyBranchShareList[index][item] ? applyBranchShareList[index][item] : ''
				const info = infoBranchShareList[index][item] ? infoBranchShareList[index][item] : ''
				if (apply !== info && item !== 'isItSuitable') { // isItSuitable为前端生成字段，用于判断预计收益是否使用
					flag = true
				}
			})
			if (flag) { // 如相同位置的数据不一样，直接返回
				if (!store.state.fundStep.pulicAttribute.includes('下属基金')) {
					store.state.fundStep.pulicAttribute.push('下属基金')
					store.state.fundStep.pulicCheckNum.baseInfo.num += 1
				}
				return true
			}
		}
	}
}

// 对比费率数组又没有变化
/*
type 费率类型
subFundType 下属基金类型
msg 文字标题
*/
export function feeRateCCompare(type, subFundType, msg) {
	// 组件初始化的时候禁止调用
	if (!store.state.fundStep.pucliktTotalData.productFundApply && !store.state.fundStep.pucliktTotalData.productFundInfo) {
		return false
	} else if (!isData(2, 'rateInfo') && !isData(1, 'rateInfo')) { // 当申请表和主表没有数据
		return false
	}
	const subFundTypeFlag = subFundType || ''
	const productFundApply = store.state.fundStep.pucliktTotalData.productFundApply
	const productFundInfo = store.state.fundStep.pucliktTotalData.productFundInfo
	const applyData = productFundApply && productFundApply.rateInfo ? productFundApply.rateInfo : {}
	const infoData = productFundInfo && productFundInfo.rateInfo ? productFundInfo.rateInfo : {}
	const applyFeeRateList = applyData.feeRateInfoList && Array.isArray(applyData.feeRateInfoList) ? applyData.feeRateInfoList : []// 申请表费率
	const infoFeeRateList = infoData.feeRateInfoList && Array.isArray(infoData.feeRateInfoList) ? infoData.feeRateInfoList : []// 主表费率
	applyFeeRateList.forEach(item => {
		item.subFundType = item.subFundType || ''
	})
	infoFeeRateList.forEach(item => {
		item.subFundType = item.subFundType || ''
	})
	const FundApplyFee = applyFeeRateList.filter(item => item.feeRateType === type && item.subFundType === subFundTypeFlag)
	const FundInfoFee = infoFeeRateList.filter(item => item.feeRateType === type && item.subFundType === subFundTypeFlag)
	if (!FundApplyFee.length && !FundInfoFee.length) { // 当申请表和主表都没有数据
		return false
	} else if (FundApplyFee.length !== FundInfoFee.length) { // 当两组数据数据长度不一样
		if (!store.state.fundStep.pulicAttribute.some(item => item === msg)) {
			store.state.fundStep.pulicAttribute.push(msg)
			store.state.fundStep.pulicCheckNum.rateInfo.num += 1
		}
		return true
	} else if (FundApplyFee.length === FundInfoFee.length) { // 当二者长度相同
		for (let index = 0; index < FundApplyFee.length; index++) {
			const keys = Object.keys(FundApplyFee[index])
			let flag = false
			keys.forEach(item => {
				const applyMsg = FundApplyFee[index][item] ? FundApplyFee[index][item] : ''
				const infoMsg = FundInfoFee[index][item] ? FundInfoFee[index][item] : ''
				if (applyMsg !== infoMsg) {
					flag = true
				}
			})
			if (flag) {
				if (!store.state.fundStep.pulicAttribute.some(item => item === msg)) {
					store.state.fundStep.pulicAttribute.push(msg)
					store.state.fundStep.pulicCheckNum.rateInfo.num += 1
				}
				return true
			}
		}
	}
}

// 对比管理、托管、销售费率及备注
/*
type 费率类型
subFundType 基金类别
msg 文字标题
*/
export function feeRateCCompareRemak(type, subFundType, msg) {
	// 组件初始化的时候禁止调用
	if (!store.state.fundStep.pucliktTotalData.productFundApply && !store.state.fundStep.pucliktTotalData.productFundInfo) {
		return false
	} else if (!isData(2, 'rateInfo') && !isData(1, 'rateInfo')) { // 当申请表和主表没有数据
		return false
	}
	const subFundTypeFlag = subFundType || ''
	const productFundApply = store.state.fundStep.pucliktTotalData.productFundApply
	const productFundInfo = store.state.fundStep.pucliktTotalData.productFundInfo
	const applyData = productFundApply && productFundApply.rateInfo ? productFundApply.rateInfo : {}
	const infoData = productFundInfo && productFundInfo.rateInfo ? productFundInfo.rateInfo : {}
	const applyFeeRateList = applyData.feeRateInfoList && Array.isArray(applyData.feeRateInfoList) ? applyData.feeRateInfoList : []// 申请表费率
	const infoFeeRateList = infoData.feeRateInfoList && Array.isArray(infoData.feeRateInfoList) ? infoData.feeRateInfoList : []// 主表费率
	applyFeeRateList.forEach(item => {
		item.subFundType = item.subFundType || ''
	})
	infoFeeRateList.forEach(item => {
		item.subFundType = item.subFundType || ''
	})
	const FundApplyFee = applyFeeRateList.filter(item => item.feeRateType === type && item.subFundType === subFundTypeFlag)
	const FundInfoFee = infoFeeRateList.filter(item => item.feeRateType === type && item.subFundType === subFundTypeFlag)
	if (!FundApplyFee.length && !FundInfoFee.length) { // 当申请表和主表都没有数据
		return false
	} else if (FundApplyFee.length !== FundInfoFee.length) { // 当两组数据数据长度不一样
		if (!store.state.fundStep.pulicAttribute.some(item => item === msg)) {
			store.state.fundStep.pulicAttribute.push(msg)
			store.state.fundStep.pulicCheckNum.rateInfo.num += 1
		}
		return true
	} else if (FundApplyFee.length === FundInfoFee.length) { // 当二者长度相同
		if (msg.includes('备注')) { // 对比费率备注
			const infoRemak = FundInfoFee[0].remark ? FundInfoFee[0].remark : ''
			const applyRemak = FundApplyFee[0].remark ? FundApplyFee[0].remark : ''
			if (infoRemak !== applyRemak) {
				if (!store.state.fundStep.pulicAttribute.some(item => item === msg)) {
					store.state.fundStep.pulicAttribute.push(msg)
					store.state.fundStep.pulicCheckNum.rateInfo.num += 1
				}
				return true
			}
		} else if (msg.includes('算法')) { // 对比费率算法
			const infosalesServiceFeeAlgorithm = FundInfoFee[0].salesServiceFeeAlgorithm ? FundInfoFee[0].salesServiceFeeAlgorithm : ''
			const applysalesServiceFeeAlgorithm = FundApplyFee[0].salesServiceFeeAlgorithm ? FundApplyFee[0].salesServiceFeeAlgorithm : ''
			if (infosalesServiceFeeAlgorithm !== applysalesServiceFeeAlgorithm) {
				if (!store.state.fundStep.pulicAttribute.some(item => item === msg)) {
					store.state.fundStep.pulicAttribute.push(msg)
					store.state.fundStep.pulicCheckNum.rateInfo.num += 1
				}
				return true
			}
		} else { // 对比费率
			const infofeeRate = FundInfoFee[0].feeRate ? FundInfoFee[0].feeRate : ''
			const applyfeeRate = FundApplyFee[0].feeRate ? FundApplyFee[0].feeRate : ''
			const infofeeRateUnit = FundInfoFee[0].feeRateUnit ? FundInfoFee[0].feeRateUnit : ''
			const applyfeeRateUnit = FundApplyFee[0].feeRateUnit ? FundApplyFee[0].feeRateUnit : ''
			if (infofeeRate !== applyfeeRate || infofeeRateUnit !== applyfeeRateUnit) {
				if (!store.state.fundStep.pulicAttribute.some(item => item === msg)) {
					store.state.fundStep.pulicAttribute.push(msg)
					store.state.fundStep.pulicCheckNum.rateInfo.num += 1
				}
				return true
			}
		}
	}
}

// 对比基金经理信息
export function fundCompare() {
	// 组件初始化的时候禁止调用
	if (!store.state.fundStep.pucliktTotalData.productFundApply && !store.state.fundStep.pucliktTotalData.productFundInfo) {
		return false
	}
	const productFundApply = store.state.fundStep.pucliktTotalData
	const productFundInfo = store.state.fundStep.pucliktTotalData
	const applyList = productFundApply && productFundApply.fundManagerApplyList ? productFundApply.fundManagerApplyList : []
	const infoList = productFundInfo && productFundInfo.fundManagerInfoList ? productFundInfo.fundManagerInfoList : []
	// 当申请表和主表都没有数据
	if (!applyList.length && !infoList.length) {
		return false
	} else if (applyList.length !== infoList.length) { // 如果二者的长度不一样，则直接返回
		if (!store.state.fundStep.pulicAttribute.includes('基金经理信息')) {
			store.state.fundStep.pulicAttribute.push('基金经理信息')
			store.state.fundStep.pulicCheckNum.fundMange.num += 1
		}
		return true
	} else if (applyList.length === infoList.length) {
		const propertyList = ['managerId', 'startDate', 'endDate', 'entryNoticeDate', 'leaveNoticeDate']// 如相同位置的数据不一样，直接返回
		for (let index = 0; index < applyList.length; index++) {
			const applyItem = applyList[index]
			const infoItem = infoList[index]
			for (let i = 0; i < propertyList.length; i++) {
				const property = propertyList[i]
				const applyItemProperty = applyItem[property] ? applyItem[property] : ''
				const infoItemProperty = infoItem[property] ? infoItem[property] : ''
				if (applyItemProperty !== infoItemProperty) {
					if (!store.state.fundStep.pulicAttribute.includes('基金经理信息')) {
						store.state.fundStep.pulicAttribute.push('基金经理信息')
						store.state.fundStep.pulicCheckNum.fundMange.num += 1
					}
					return true
				}
			}
		}
	}
}

// 对比文件信息
// type 文件类型
// msg 文字标题
export function fileCompare(type, msg) {
	// 组件初始化的时候禁止调用
	if (!store.state.fundStep.pucliktTotalData.productFundApply && !store.state.fundStep.pucliktTotalData.productFundInfo) {
		return false
	}
	// 当申请表和主表都没有数据
	if (!isData(1, 'fundFileInfo') && !isData(2, 'fundFileInfo')) {
		return false
	}
	const productFundApply = store.state.fundStep.pucliktTotalData.productFundApply
	const productFundInfo = store.state.fundStep.pucliktTotalData.productFundInfo
	const applyData = productFundApply && productFundApply.fundFileInfo ? productFundApply.fundFileInfo : {}
	const infoData = productFundInfo && productFundInfo.fundFileInfo ? productFundInfo.fundFileInfo : {}
	const applyKeys = applyData[type] ? Object.keys(applyData[type]) : []
	const infoKeys = infoData[type] ? Object.keys(infoData[type]) : []
	if (!applyKeys.length && !infoKeys.length) { // 属性列表为空，直接返回
		return false
	} else if (applyKeys.length || infoKeys.length) {
		const lengthFlag = applyKeys.length < infoKeys.length
		const list = lengthFlag ? infoKeys : applyKeys
		for (let index = 0; index < list.length; index++) {
			// 防止空属性作为对象
			const infoObj = infoData[type] ? infoData[type] : {}
			const applyObj = applyData[type] ? applyData[type] : {}
			const infoProperty = infoObj[list[index]] ? infoObj[list[index]] : ''
			const applyProperty = applyObj[list[index]] ? applyObj[list[index]] : ''
			if (infoProperty !== applyProperty) { // 对比值不相同，直接返回
				if (!store.state.fundStep.pulicAttribute.includes(msg)) {
					store.state.fundStep.pulicAttribute.push(msg)
					store.state.fundStep.pulicCheckNum.fileInfo.num += 1
				}
				return true
			}
		}
	}
}

// 变更保存时将页面数据与主表数据做对比
export function updateCompareSubmit() {
	publiArraycCompare() // 下属基金
	baseInfo.forEach(item => { /* 1.基本信息对比*/
		publicCompare(item.key, item.value, 'baseInfo')
	})
	dateInfo.forEach(item => { /* 2.对比日期*/
		publicCompare(item.key, item.value, 'dateInfo')
	})
	organInfo.forEach(item => { /* 3.对比机构信息*/
		publicCompare(item.key, item.value, 'organInfo')
	})
	investInfo.forEach(item => { /* 5.对比投资相关信息*/
		publicCompare(item.key, item.value, 'investInfo')
	})
	fundCompare()// 对比基金经理
	fundFileInfo.forEach(item => { // 对比文件信息
		item.typeName.forEach(items => {
			fileCompare(items.objName, `${item.type}-${items.title}`)
		})
	})
	rateInfo.push({ children: feeRateContrast() })
	rateInfo.forEach(item => { // 4.对比费率
		if (!item.children) {
			publicCompare(item.key, item.value, 'rateInfo')
		} else {
			item.children.forEach(items => { // 对比费率列表
				if (['7', '8', '9'].includes(items.feeRateType)) {
					feeRateCCompareRemak(items.feeRateType, items.subFundType, items.msg)
				} else {
					feeRateCCompare(items.feeRateType, items.subFundType, items.msg)
				}
			})
		}
	})
}
